const Koa = require('koa');
const KoaRouter = require('koa-router');
const KoaBodyparser = require('koa-bodyparser');
const KoaStatic = require('koa-static');
const send = require('koa-send');
const Mock = require('mockjs');
const mongoose = require('mongoose');
const cors = require('@koa/cors');
const fs = require('fs');
const jwt = require("jsonwebtoken");

const app = new Koa();
const router = new KoaRouter();

app.use(cors());
app.use(KoaBodyparser({
	multipart: true,
	formLimit: "10mb",
	jsonLimit: "10mb",
	textLimit: "10mb",
	enableTypes: ['json', 'form', 'text']
}));
app.use(KoaStatic('./public'));
app.use(router.routes()).use(router.allowedMethods());

const {getWebTags,getWebTypes} = require('./controls/article/common.js');
const {delImg,addWebTags,delWebTags,changeWebType,delWebType,addWebType,saveWebImg,getWebImgs,publishWebArticle,delArticleMessage,delArticleById,updateArticle,addArticleTag,addArticleType,delArticleTag,delArticleType} = require('./controls/article/backStage.js');
const {getArticleByPage,getWebArticleById,getArticleByTag,getWebArticleByType,getWebPageByViews,getWebPageByTime,getArticleMessage,publishArticleMessage,searchArticle,getAllArticle,getAllTypePage,getAllTagPage} = require('./controls/article/frontStage.js');

const {changeSeoInfo,getSeoInfo,changeFooterInfo,getFooterInfo,delFriendLink,addFriendLink,changeFriendLink,getFriendLinks,getBanner,changeBanner,getSpicalColumn,changeSpicalColumn,getAuthorInfo,changeAuthorInfo,getRecommend,changeRecommend} = require('./controls/home/index.js');

const {getHtmlDir,addHtmlFile,getJsDir} = require('./controls/example/index.js');

const {login,changePassword,verify} = require('./controls/login/index.js');

//访问图片
router.get('/api/imgs/:filename',async ctx=>{
	const filename = ctx.params.filename;
	const fullpath = `./data/imgs/${filename}`;
	ctx.attachment(fullpath);
	await send (ctx,fullpath)
});
//访问html文件
router.get('/api/examples/:filename',async ctx=>{
	const filename = ctx.params.filename;
	const fullpath = `./data/exampleDir/${filename}`;
	ctx.attachment(fullpath);
	await send (ctx,fullpath)
});
//访问js文件
router.get('/api/threejs/:dirname/:filename',async ctx=>{
	const dirname = ctx.params.dirname;
	const filename = ctx.params.filename;
	let fullpath = `./data/jsDir/threeJs/${dirname}/${filename}`;
	ctx.attachment(fullpath);
	await send (ctx,fullpath)
});

//文章管理与访问
router.get('/api/getwebpage',getArticleByPage);//获取webpage信息
router.get('/api/getwebtypes',getWebTypes);//获取webtype信息
router.get('/api/getwebtags',getWebTags);//获取webtag信息
router.get('/api/getwebarticle',getWebArticleById);//获取web文章
router.get('/api/getwebpagebytag',getArticleByTag);//根据tag获取文章
router.get('/api/getwebpagebytype',getWebArticleByType);//根据分类获取文章
router.get('/api/getwebpagebyviews',getWebPageByViews);//获取阅读最多的文章
router.get('/api/getwebpagebytime',getWebPageByTime);//获取最新发布的文章
router.get('/api/getarticlemessage',getArticleMessage);//获取文章对应的评论
router.post('/api/publisharticlemessage',publishArticleMessage);
router.post('/api/delarticlemessage',delArticleMessage);
router.post('/api/searcharticle',searchArticle);
router.get('/api/getallarticle',getAllArticle);
router.get('/api/getalltypepage',getAllTypePage);
router.get('/api/getalltagpage',getAllTagPage);

router.post('/api/addwebtype',addWebType);//添加web文章分类
router.post('/api/delwebtype',delWebType);//删除web文章分类
router.post('/api/changewebtype',changeWebType);//修改web文章分类
router.post('/api/publishwebarticle',publishWebArticle);//添加web文章
router.post('/api/savewebimg',saveWebImg);//保存web的图片
router.get('/api/getwebimg',getWebImgs);//获取web图片
router.post('/api/delimg',delImg);//删除图片
router.get('/api/getcontrolwebtags',getWebTags);//获取web标签
router.post('/api/addwebtag',addWebTags);//添加新的tag
router.post('/api/delwebtag',delWebTags);//删除tag
router.post('/api/delarticlebyid',delArticleById);//删除文章
router.post('/api/updatearticle',updateArticle);//更新文章
router.post('/api/addarticletag',addArticleTag);//给文章加标签
router.post('/api/addarticletype',addArticleType);//给文章加分类
router.post('/api/delarticletag',delArticleTag);
router.post('/api/delarticletype',delArticleType);
router.get('/api/getfooterinfo',getFooterInfo);//获取底部信息
router.post('/api/changefooterinfo',changeFooterInfo);//修改底部信息

//首页管理与访问
router.get('/api/getbanner',getBanner);//获取首页轮播图信息
router.post('/api/changebanner',changeBanner);//修改首页轮播图
router.get('/api/getspicalcolumn',getSpicalColumn);//获取专栏信息
router.post('/api/changespicalcolumn',changeSpicalColumn);//修改专栏信息
router.get('/api/getauthorinfo',getAuthorInfo);//获取作者信息
router.post('/api/changeauthorinfo',changeAuthorInfo);//修改作者信息
router.get('/api/getrecommend',getRecommend);//获取推荐文章
router.post('/api/changerecommend',changeRecommend);//修改推荐文章
router.get('/api/getfriendlinks',getFriendLinks);//获取友情链接
router.post('/api/changefriendlink',changeFriendLink);//修改友情链接
router.post('/api/addfriendlink',addFriendLink);//添加友情链接
router.post('/api/delfriendlink',delFriendLink);//删除友情链接
router.get('/api/getseoinfo',getSeoInfo);//获取seo信息
router.post('/api/changeseoinfo',changeSeoInfo);//修改seo信息

//案例
router.get('/api/gethtmldir',getHtmlDir);//获取文件夹
router.post('/api/addhtmlfile',addHtmlFile);//添加文件
router.get('/api/getjsdir',getJsDir);//获取js文件夹

//登录
router.get('/api/verify',verify);//token验证
router.post('/api/login',login);//账号登录
router.post('/api/changepassword',changePassword);//修改密码


mongoose.connect('mongodb://localhost:27017/boke',{
	useNewUrlParser:true,
	useUnifiedTopology:true
}).then(()=>{
	console.log('数据库连接成功!');
	app.listen(3000,()=>{
		console.log('监听3000端口');
	});
}).catch(()=>{
	console.log('数据库连接失败!');
});


